package com.mswyt.utils.test.thread;

import com.mswyt.utils.msConverts.MsConvertDataUtils;
import com.mswyt.utils.msThread.MsThreadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @ClassName: ValidatedTestController
 * @Description:
 */
@RestController
@RequestMapping("/thread")
@Validated
public class MsThreadController {
    private static String executorName1 = "one";
    private static String executorName2 = "two";

    @Autowired
    private MsThreadService threadService;

    /**
     * 测试@MsAsync注解，开启异步
     **/
    @GetMapping("/test1")
    public void test1() {

        for (int i = 0; i < 10; i++) {
            threadService.testMsAsync(i);
        }

    }


    @GetMapping("/test2")
    public void test2() {
        MsThreadUtils.getCompletionService(executorName2);
    }

    /**
     * 无顺序的获取返回值
     **/
    @GetMapping("/test3")
    public String test3() throws Exception {
        List<String> resultList = new ArrayList<>();
        MsThreadUtils.shutdown(executorName1);
        CompletionService<String> completionService = MsThreadUtils.getCompletionService(executorName1);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            completionService.submit(() -> {
                int miss = new Random().nextInt(300) + 500;
                MsThreadUtils.threadSleep(miss);
                return "i = " + finalI + " ，time = " + miss;
            });
        }
        System.out.println(" -----主线程：" + Thread.currentThread().getName() + "---线程id : " + Thread.currentThread().getId() + "----  ");
        for (int i = 0; i < 10; i++) {
            String result = completionService.take().get();
            System.out.println(result);
            resultList.add(result);
        }

        return MsConvertDataUtils.objToJsonStr(resultList);
    }

    /**
     * 有顺序的获取返回值
     **/
    @GetMapping("/test4")
    public void test4() throws Exception {
        ExecutorService futureExecutor = MsThreadUtils.getExecutorService(executorName1);
        List<Future<String>> futureList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            futureList.add(futureExecutor.submit(() -> {
                int miss = new Random().nextInt(500) + 1000;
                MsThreadUtils.threadSleep(miss);
                System.out.println(finalI + " : " + miss);
                return Thread.currentThread().getId() + "---" + finalI;
            }));
        }
        for (Future<String> future : futureList) {
            System.out.println(MsThreadUtils.getResult(future, 700));
        }
        MsThreadUtils.shutdown(executorName1);
    }


    /**
     * 判断一个线程在指定时间内是否可以执行完毕。
     **/
    @GetMapping("/test5")
    public void test5() throws Exception {
        CompletionService<String> completionService = MsThreadUtils.getCompletionService(executorName1);
        completionService.submit(() -> {
            MsThreadUtils.threadSleep(2000);
            return "completionService睡眠两秒";
        });
        //String result = completionService.take().get();
        //如果线程在1900ms内没有返回结果，判断执行超时
        String result = MsThreadUtils.getResult(completionService, 2100);
        System.err.println(result);

        //future对象
        ExecutorService executorService = MsThreadUtils.getExecutorService(executorName1);
        Future<String> future = executorService.submit(() -> {
            MsThreadUtils.threadSleep(2000);
            return "executorService睡眠两秒";
        });
        String result2 = MsThreadUtils.getResult(future, 1800);
        System.err.println(result2);
    }
}
